package com.yanqu.road.logic.config.unioncampwar;

import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.entity.unioncampwar.config.UnionCampWarBuffConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.*;

public class UnionCampWarConfig {
    private Map<Integer, UnionCampWarCenterConfig> centerConfigMap = new HashMap<>();
    /**
     * 点赞初始随机范围
     */
    private List<Integer> worshipRandomList = new ArrayList<>();
//    /**
//     * 次数初始值
//     */
//    private int attackTimesInit = 3;
//    /**
//     * 次数恢复CD秒
//     */
//    private int attackTimesRecoverCD = 300;
//    /**
//     * 次数恢复值
//     */
//    private int attackTimesRecoverValue = 3;
    /**
     * 次数回复时间点,秒
     */
    private List<Long> attackTimesRecoverTimeList = new ArrayList<>();
    /**
     * 进攻次数上限
     */
    private int attackTimesMax = 10;
    /**
     * 每个门客进攻次数上限,免费的上限
     */
    private int patronsAttackLimit = 1;
    /**
     * 出战门客数上限
     */
    private int attackPatronsNum = 6;
    /**
     * 前N生成据点
     */
    private int topUnionCount = 10;
    /**
     * 公告数量上限
     */
    private int noticeNumMax = 50;
    /**
     * 公告需要击败门客数
     */
    private int noticeNeedDefeatNum = 100;
    /**
     * 阵营总血量
     */
    private int campHp = 100000;
    /**
     * 排名对应血量权重
     */
    private List<UnionCampWarHoldConfig> holdConfigList = new ArrayList<>();
//    /**
//     * 个人排名胜方奖励
//     */
//    private List<UnionCampWarRankRewardInfo> userRankWinRewradList = new ArrayList<>();
//    /**
//     * 个人排名败方奖励
//     */
//    private List<UnionCampWarRankRewardInfo> userRankLoseRewradList = new ArrayList<>();

    private List<Integer> hpParams1 = new ArrayList<>();
    private List<Integer> hpParams2 = new ArrayList<>();
    private List<List<Integer>> hpParams3 = new ArrayList<>();
    private List<Integer> hpParams4 = new ArrayList<>();
    private List<List<Integer>> combAttList = new ArrayList<>();

    private String SHANGBANG_APPLY_TIME;
    private String SHANGBANG_ATTACK_TIME;
    private String SHANGBANG_REWARD_TIME;
    private String SHANGBANG_TITLE_SET;
    private String SHANGBANG_BUFF;
    /**
     * 当前周的报名期开始时间
     */
    private long nowWeekRegistrationTime;
    /**
     * 当前周的开战开始时间
     */
    private long nowWeekBattleTime;
    /**
     * 领奖期开始时间
     */
    private long nowWeekRewardTime;
    /**
     * 结束时间
     */
    private long nowWeekEndTime;

    /**
     * 下一周的起始时间
     */
    private long nextWeekTime;
    private Property lotteryCost = new Property();
    /**
     * 宝箱数
     */
    private int boxNum = 100;

    /**
     * 聊天上限
     */
    private int chatNumMax = 100;
    /**
     * 打第一名需要阵营血量低于配置千分比
     */
//    private int firstNeedCampHpPercent = 500;
    /**
     * 击败门客数对应的扣血
     */
//    private List<UnionCampWarDefeatNumReward> defeatNumRewardList = new ArrayList<>();

    /**
     * buff配置
     */
    private Map<Integer, UnionCampWarBuffConfig> buffConfigMap = new HashMap<>();

    public void setSHANGBANG_APPLY_TIME(String SHANGBANG_APPLY_TIME) {
        this.SHANGBANG_APPLY_TIME = SHANGBANG_APPLY_TIME;
    }

    public void setSHANGBANG_ATTACK_TIME(String SHANGBANG_ATTACK_TIME) {
        this.SHANGBANG_ATTACK_TIME = SHANGBANG_ATTACK_TIME;
    }

    public void setSHANGBANG_REWARD_TIME(String SHANGBANG_REWARD_TIME) {
        this.SHANGBANG_REWARD_TIME = SHANGBANG_REWARD_TIME;
    }

    public void setSHANGBANG_LOTTERY_COST(String SHANGBANG_LOTTERY_COST) {
        this.lotteryCost = PropertyHelper.parseStringToProperty(SHANGBANG_LOTTERY_COST);
    }

    public void setSHANGBANG_TITLE_SET(String SHANGBANG_TITLE_SET) {
        this.SHANGBANG_TITLE_SET = SHANGBANG_TITLE_SET;
    }

    public void setSHANGBANG_BUFF(String SHANGBANG_BUFF) {
        this.SHANGBANG_BUFF = SHANGBANG_BUFF;
    }

    public List<Integer> getHpParams1() {
        return hpParams1;
    }

    public void setHpParams1(List<Integer> hpParams1) {
        this.hpParams1 = hpParams1;
    }

    public List<Integer> getHpParams2() {
        return hpParams2;
    }

    public void setHpParams2(List<Integer> hpParams2) {
        this.hpParams2 = hpParams2;
    }

    public List<List<Integer>> getHpParams3() {
        return hpParams3;
    }

    public void setHpParams3(List<List<Integer>> hpParams3) {
        this.hpParams3 = hpParams3;
    }

    public List<Integer> getHpParams4() {
        return hpParams4;
    }

    public void setHpParams4(List<Integer> hpParams4) {
        this.hpParams4 = hpParams4;
    }

    public List<List<Integer>> getCombAttList() {
        return combAttList;
    }

    public int getCombAttValue(int winNum) {
        int value = 0;
        for (List<Integer> combAtt : combAttList) {
            int minValue = combAtt.get(0);
            if (winNum < minValue) {
                break;
            }
            value = combAtt.get(1);
        }
        return value;
    }

    public void setCombAttList(List<List<Integer>> combAttList) {
        this.combAttList = combAttList;
    }

    /**
     * 周几到周一零点的秒数
     *
     * @param weekDay 1-7
     * @return
     */
    private long parseWeekDayDiffSecond(int weekDay) {
        if (weekDay == 0) {
            weekDay = 7;
        }
        return (weekDay - 1) * DateHelper.DAY_SECONDS;
    }

    public void initConfig() {
        initWeekTime();

        String[] strAttrs = SHANGBANG_TITLE_SET.split("\\|");
        for (int i = 0; i < strAttrs.length; i++) {
            String strAttr = strAttrs[i];
            List<Integer> attrs = StringUtils.stringToIntegerList(strAttr, ";");
            UnionCampWarHoldConfig holdConfig = new UnionCampWarHoldConfig(i);
            holdConfig.setStartHoldId(attrs.get(0));
            holdConfig.setHp(attrs.get(1));
            holdConfig.setPatronsSubHp(attrs.get(2));
            holdConfig.setPatronsAddScore(attrs.get(3));
            this.holdConfigList.add(holdConfig);
        }
        int maxRank = this.topUnionCount;
        //倒序遍历
        for (int i = this.holdConfigList.size() - 1; i >= 0; i--) {
            UnionCampWarHoldConfig holdConfig = this.holdConfigList.get(i);
            holdConfig.setEndHoldId(maxRank);
            maxRank = holdConfig.getStartHoldId() - 1;
        }

        strAttrs = SHANGBANG_BUFF.split("\\|");
        for (int i = 0; i < strAttrs.length; i++) {
            UnionCampWarBuffConfig buffConfig = new UnionCampWarBuffConfig(i + 1, strAttrs[i]);
            this.buffConfigMap.put(buffConfig.getBuffId(), buffConfig);
        }
    }

    public int getTopUnionCount() {
        return topUnionCount;
    }

    public void setTopUnionCount(int topUnionCount) {
        this.topUnionCount = topUnionCount;
    }


    /**
     * 胜利场次对应的增加积分
     *
     * @param winNum
     * @return
     */
    public int getWinAddUserScore(int holdId, int winNum) {
        UnionCampWarHoldConfig holdConfig = getHoldConfig(holdId);
        if (null == holdConfig) {
            return winNum;
        }
        return winNum * holdConfig.getPatronsAddScore();
    }

    public String getBattleRewards(int centerGroupId, int holdId, int winNum) {
        UnionCampWarCenterConfig centerConfig = getCenterConfig(centerGroupId);
        if (null == centerConfig) {
            return "";
        }
        return centerConfig.getBattleRewards(holdId, winNum);
    }

    private void initWeekTime() {
        long weekStartTime = DateHelper.getThisWeekStartTime(GameConfig.DAY_SYSTEM_RESET_TIME) / 1000;
        this.nextWeekTime = weekStartTime + DateHelper.WEEK_SECONDS;
        long nowWeekZeroTime = weekStartTime - (GameConfig.DAY_SYSTEM_RESET_TIME - 1) * DateHelper.DAY_SECONDS;
        List<Integer> weekTimes = StringUtils.stringToIntegerList(SHANGBANG_APPLY_TIME, ";");
        this.nowWeekRegistrationTime = nowWeekZeroTime + parseWeekDayDiffSecond(weekTimes.get(0)) + DateHelper.HOUR_SECONDS * weekTimes.get(1);
        weekTimes = StringUtils.stringToIntegerList(SHANGBANG_ATTACK_TIME, ";");
        this.nowWeekBattleTime = nowWeekZeroTime + parseWeekDayDiffSecond(weekTimes.get(0)) + DateHelper.HOUR_SECONDS * weekTimes.get(1);
        String[] strAttrs = SHANGBANG_REWARD_TIME.split("\\|");
        weekTimes = StringUtils.stringToIntegerList(strAttrs[0], ";");
        this.nowWeekRewardTime = nowWeekZeroTime + parseWeekDayDiffSecond(weekTimes.get(0)) + DateHelper.HOUR_SECONDS * weekTimes.get(1);
        weekTimes = StringUtils.stringToIntegerList(strAttrs[1], ";");
        this.nowWeekEndTime = nowWeekZeroTime + parseWeekDayDiffSecond(weekTimes.get(0)) + DateHelper.HOUR_SECONDS * weekTimes.get(1);
    }

    private void checkWeekTime() {
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime < this.nextWeekTime) {
            return;
        }
        initWeekTime();
    }

    /**
     * 是否在报名期
     *
     * @return
     */
    public boolean isInRegistrationTime() {
        checkWeekTime();
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime >= this.nowWeekRegistrationTime && nowTime < this.nowWeekBattleTime) {
            return true;
        }
        return false;
    }

    /**
     * 是否报名截止前5分钟
     *
     * @return
     */
    public boolean isRegistrationEndTime() {
        checkWeekTime();
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime >= this.nowWeekBattleTime - DateHelper.MINUTE_SECONDS * 5 && nowTime < this.nowWeekEndTime) {
            return true;
        }
        return false;
    }

    /**
     * 是否在开战期
     *
     * @return
     */
    public boolean isInBattleTime() {
        checkWeekTime();
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime >= this.nowWeekBattleTime && nowTime < this.nowWeekRewardTime) {
            return true;
        }
        return false;
    }

    /**
     * 是否在领奖期
     *
     * @return
     */
    public boolean isInRewardTime() {
        checkWeekTime();
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime >= this.nowWeekRewardTime && nowTime < this.nowWeekEndTime) {
            return true;
        }
        return false;
    }

    /**
     * 榜单补发时间
     *
     * @return
     */
    public boolean isAutoRankRewardTime() {
        checkWeekTime();
        long nowTime = DateHelper.getCurrentSecond();
        if (nowTime >= this.nowWeekEndTime || nowTime < this.nowWeekRegistrationTime) {
            return true;
        }
        return false;
    }

    public long getNowWeekRegistrationTime() {
        checkWeekTime();
        return nowWeekRegistrationTime;
    }

    public long getNowWeekBattleTime() {
        checkWeekTime();
        return nowWeekBattleTime;
    }

    public long getNowWeekRewardTime() {
        checkWeekTime();
        return nowWeekRewardTime;
    }

    public long getNowWeekEndTime() {
        checkWeekTime();
        return nowWeekEndTime;
    }

    public int getBoxNum() {
        return boxNum;
    }


    public List<PropertyWeightItem> getGeneralLotteryList(int centerGroupId) {
        UnionCampWarCenterConfig centerConfig = getCenterConfig(centerGroupId);
        if (null == centerConfig) {
            return new ArrayList<>();
        }
        return centerConfig.getGeneralLotteryList();
    }

    public List<PropertyWeightItem> getSpecialLotteryList(int centerGroupId) {
        UnionCampWarCenterConfig centerConfig = getCenterConfig(centerGroupId);
        if (null == centerConfig) {
            return new ArrayList<>();
        }
        return centerConfig.getSpecialLotteryList();
    }

    public Property getLotteryCost() {
        return lotteryCost;
    }

    public int getAttackTimesMax() {
        return attackTimesMax;
    }

    public void setAttackTimesMax(int attackTimesMax) {
        this.attackTimesMax = attackTimesMax;
    }

    public int getCampHp() {
        return campHp;
    }

    public void setCampHp(int campHp) {
        this.campHp = campHp;
    }


    public int getHoldHp(int rank) {
        UnionCampWarHoldConfig holdConfig = getHoldConfig(rank);
        if (null == holdConfig) {
            return 0;
        }
        return holdConfig.getHp();
    }

    public UnionCampWarBuffConfig getBuffConfig(int buffId) {
        return buffConfigMap.get(buffId);
    }

    public int getNoticeNeedDefeatNum() {
//        return 1;
        return noticeNeedDefeatNum;
    }

    public void setNoticeNeedDefeatNum(int noticeNeedDefeatNum) {
        this.noticeNeedDefeatNum = noticeNeedDefeatNum;
    }

    public int getDefeatNumSubHp(int holdId, int defeatNum) {
//        UnionCampWarHoldConfig holdConfig = getHoldConfig(holdId);
//        if (null == holdConfig) {
//            return defeatNum;
//        }
//        return defeatNum * holdConfig.getPatronsSubHp();
        return getCombAttValue(defeatNum);
    }

    public int getNoticeNumMax() {
        return noticeNumMax;
    }

    public void setNoticeNumMax(int noticeNumMax) {
        this.noticeNumMax = noticeNumMax;
    }

    public int getPatronsAttackLimit() {
        return patronsAttackLimit;
    }

    public void setPatronsAttackLimit(int patronsAttackLimit) {
        this.patronsAttackLimit = patronsAttackLimit;
    }

    public int getAttackPatronsNum() {
        return attackPatronsNum;
    }

    public void setAttackPatronsNum(int attackPatronsNum) {
        this.attackPatronsNum = attackPatronsNum;
    }

    public void setBoxNum(int boxNum) {
        this.boxNum = boxNum;
    }

//    public int getAttackTimesInit() {
//        return attackTimesInit;
//    }
//
//    public void setAttackTimesInit(int attackTimesInit) {
//        this.attackTimesInit = attackTimesInit;
//    }

//    public int getAttackTimesRecoverCD() {
//        return attackTimesRecoverCD;
//    }
//
//    public void setAttackTimesRecoverCD(int attackTimesRecoverCD) {
//        this.attackTimesRecoverCD = attackTimesRecoverCD;
//    }

//    public int getAttackTimesRecoverValue() {
//        return attackTimesRecoverValue;
//    }
//
//    public void setAttackTimesRecoverValue(int attackTimesRecoverValue) {
//        this.attackTimesRecoverValue = attackTimesRecoverValue;
//    }


    public List<Long> getAttackTimesRecoverTimeList() {
        return attackTimesRecoverTimeList;
    }

    public void setAttackTimesRecoverTimeList(List<Long> attackTimesRecoverTimeList) {
        this.attackTimesRecoverTimeList = attackTimesRecoverTimeList;
    }

    public UnionCampWarHoldConfig getHoldConfig(int holdId) {
        if (holdId <= 0) {
            return null;
        }
        UnionCampWarHoldConfig takeConfig = null;
        for (UnionCampWarHoldConfig holdConfig : this.holdConfigList) {
            if (holdId < holdConfig.getStartHoldId()) {
                break;
            }
            takeConfig = holdConfig;
        }
        return takeConfig;
    }

    /**
     * 随机据点的路线
     *
     * @return
     */
    public Map<Integer, List<Integer>> getRoteHoldList() {
        Map<Integer, List<Integer>> routeMap = new HashMap<>();
        routeMap.put(this.holdConfigList.get(0).getStartHoldId(), new ArrayList<>());
        //路线数量相当于主将数量
        int routeNum = this.holdConfigList.get(2).getStartHoldId() - this.holdConfigList.get(1).getStartHoldId();
        List<Integer> holdIdList = new ArrayList<>();
        for (int i = this.holdConfigList.get(2).getStartHoldId(); i <= this.topUnionCount; i++) {
            holdIdList.add(i);
        }

        //打乱
        Collections.shuffle(holdIdList);

        for (int i = 0; i < routeNum; i++) {
            routeMap.put(i, new ArrayList<>());
            routeMap.get(i).add(1);//路线起点固定为统帅
            routeMap.get(i).add(i + 2);//二级点固定为路线数的统帅下一级

            int linkCount = (holdIdList.size() + routeNum - 1) / routeNum;//向上取整
            for (int j = 0; j < linkCount; j++) {
                if ((j * routeNum + i) < holdIdList.size()) {
                    Integer holdId = holdIdList.get(j * routeNum + i);
                    routeMap.get(i).add(holdId);
                }
            }
        }

        //排序从大到小
        for (Map.Entry<Integer, List<Integer>> entry : routeMap.entrySet()) {
            entry.getValue().sort((o1, o2) -> Integer.compare(o2, o1));
        }
        return routeMap;
    }

    public UnionCampWarHoldConfig getNextHoldConfig(UnionCampWarHoldConfig holdConfig) {
        int nextIdx = holdConfig.getIndex() + 1;
        if (nextIdx >= this.holdConfigList.size()) {
            return null;
        }
        return this.holdConfigList.get(nextIdx);
    }

    public String getCampRewards(int centerGroupId, boolean isWin) {
        UnionCampWarCenterConfig centerConfig = getCenterConfig(centerGroupId);
        if (centerConfig == null) {
            return "";
        }
        return centerConfig.getCampRewards(isWin);
    }

    public int getChatNumMax() {
        return chatNumMax;
    }

    public void setChatNumMax(int chatNumMax) {
        this.chatNumMax = chatNumMax;
    }

    /**
     * 后台配置调用这个初始化
     *
     * @param key
     * @param value
     */
    public void initCenterConfig(int centerGroupId, String key, String value) {

        UnionCampWarCenterConfig centerConfig = centerConfigMap.get(centerGroupId);
        if (centerConfig == null) {
            centerConfig = new UnionCampWarCenterConfig(centerGroupId);
            centerConfigMap.put(centerGroupId, centerConfig);
        }
        centerConfig.initCenterConfig(key, value);
    }

    public UnionCampWarCenterConfig getCenterConfig(int centerGroupId) {
        return centerConfigMap.get(centerGroupId);
    }

//    public int getFirstNeedCampHpPercent() {
//        return firstNeedCampHpPercent;
//    }
//
//    public void setFirstNeedCampHpPercent(int firstNeedCampHpPercent) {
//        this.firstNeedCampHpPercent = firstNeedCampHpPercent;
//    }

    public boolean isRecoverAttackTimes(long lastAttackTime) {
        int currentSecond = DateHelper.getCurrentSecond();
        for (Long checkTime : this.attackTimesRecoverTimeList) {
            long compareTime = this.nowWeekBattleTime + checkTime;
            if (lastAttackTime / DateHelper.SECOND_MILLIONS < compareTime && currentSecond >= compareTime) {
                return true;
            }
        }
        return false;
    }

    public List<UnionCampWarHoldConfig> getHoldConfigList() {
        return holdConfigList;
    }

    public void setWorshipRandomList(List<Integer> worshipRandomList) {
        this.worshipRandomList = worshipRandomList;
    }

    public int getWorshipInitNum() {
        Integer minNum = worshipRandomList.get(0);
        Integer maxNum = worshipRandomList.get(1);
        long randomNum = RandomHelper.nextLong(maxNum - minNum);

        return minNum + (int) randomNum;
    }

    public Map<Integer, UnionCampWarBuffConfig> getBuffConfigMap() {
        return buffConfigMap;
    }
}
